package com.fary.wechat;

import com.fary.delegate.WxMpMessageDelegate;
import com.fary.mapper.PayMerchantInfoMapper;
import com.fary.service.PayMerchantQrCodeService;
import com.fary.wechat.builder.TextBuilder;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class PayWeChatDelegate implements WxMpMessageDelegate {

    @Autowired
    private PayMerchantInfoMapper payMerchantInfoMapper;
    @Autowired
    private PayMerchantQrCodeService payMerchantQrCodeService;

    @Override
    public WxMpXmlOutMessage handlerUnsubscribe(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) {
        String openId = wxMessage.getFromUser();
        payMerchantInfoMapper.updateContactWxOpenid(openId);
        return null;
    }

    @Override
    public WxMpXmlOutMessage handlerScan(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) {
        // 扫码事件处理
        boolean scanningRelevanceResult = payMerchantQrCodeService.scanningRelevanceOpenId(wxMessage);
        String content = scanningRelevanceResult ? "关联成功" : "关联失败";
        return new TextBuilder().build(content, wxMessage, wxMpService);
    }
}
